package com.study.testSpringBoot.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {

	@Autowired
	private RedisUtil redisUtil;
	
	volatile int flag = 0;
	
	@GetMapping("/test")
	public String test() {
		Object object = redisUtil.get("k2");
		System.out.println(object);
		return "SUCCESS";
	}
	
	@GetMapping("/testRedisLock")
	public String testRedisLock() {
		String key = "test";
		while(!redisUtil.setNx(key)) {
			
		}
		flag++;
		System.out.println("当前数量："+flag);
		redisUtil.del("redis_lock_"+key);
		return "SUCCESS";
	}
	
	@GetMapping("/getFlag")
	public String getFlag() {
		return "当前记录数："+flag;
	}
}
